package com.lmabbe.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.objects.converter.ModuleConverter;
import com.lmabbe.tenant.objects.converter.ModuleResourceConverter;
import com.lmabbe.tenant.objects.dto.ModuleDTO;
import com.lmabbe.tenant.objects.dto.ModuleResourceDTO;
import com.lmabbe.tenant.objects.entity.Module;
import com.lmabbe.tenant.objects.entity.ModuleResource;
import com.lmabbe.tenant.objects.query.ModuleQuery;
import com.lmabbe.tenant.objects.vo.ModuleTreeVO;
import com.lmabbe.tenant.objects.vo.ModuleVO;
import com.lmabbe.tenant.objects.vo.ModuleResourceVO;
import com.lmabbe.tenant.service.ModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统模块表管理
 *
 * @author lmabbe
 */
@Api(tags = "系统模块表管理")
@RestController
@RequestMapping("module")
@RequiredArgsConstructor
public class ModuleController {

    private final ModuleService moduleService;
    private final ModuleConverter moduleConverter;
    private final ModuleResourceConverter ModuleResourceConverter;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<ModuleVO>> list() {
        List<Module> list = moduleService.list();
        return R.success(moduleConverter.list2Vos(list));
    }

    /**
     * 获取所有树形结构
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("allTree")
    public R<List<ModuleTreeVO>> allTree() {
        return R.success(moduleService.allTree());
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<ModuleVO>> list(ModuleQuery query) {
        IPage page = moduleService.page(query.createPage(), query.createQuery());
        page.setRecords(moduleConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<ModuleVO> getById(@PathVariable Long id) {
        return R.success(moduleConverter.entity2Vo(moduleService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody ModuleDTO dto) {
        this.moduleService.save(moduleConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("/{moduleId}")
    public R update(@RequestBody ModuleDTO dto, @PathVariable Long moduleId) {
        this.moduleService.updateById(moduleConverter.dto2Entity(dto, moduleId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.moduleService.removeById(id);
        return R.success();
    }

    /**
     * 获取模块资源
     *
     * @return
     */
    @Operation(summary = "获取模块资源")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "moduleId", value = "模块ID")
    })
    @GetMapping("/{moduleId}/resources")
    public R<List<ModuleResourceVO>> resources(@PathVariable Long moduleId) {
        return R.success(moduleService.resources(moduleId));
    }


    @Operation(summary = "获取模块资源的子级")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "parentId", value = "上级资源ID")
    })
    @GetMapping("/{parentId}/childResources")
    public R<List<ModuleResourceVO>> childResource(@PathVariable Long parentId) {
        return R.success(moduleService.childResources(parentId));
    }

    /**
     * 获取资源详情
     *
     * @param resourceId
     * @return
     */
    @GetMapping("/resource/{resourceId}")
    @Operation(summary = "获取资源详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "resourceId", value = "资源ID")
    })
    public R resource(@PathVariable Long resourceId) {
        return R.success(ModuleResourceConverter.entity2Vo(moduleService.resource(resourceId)));
    }

    /**
     * 删除模块资源
     */
    @Operation(summary = "删除模块资源")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "resourceId", value = "资源ID")
    })
    @DeleteMapping("resource/{resourceId}")
    public R delResource(@PathVariable Long resourceId) {
        moduleService.delResource(resourceId);
        return R.success();
    }

    /**
     * 新增模块资源
     */
    @Operation(summary = "新增模块资源")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "resourceId", value = "资源ID"),
            @ApiImplicitParam(name = "resource", value = "资源信息")
    })
    @PostMapping("{moduleId}/resource")
    public R addResource(@PathVariable Long moduleId, @RequestBody ModuleResourceDTO resourceDTO) {
        moduleService.addResource(ModuleResourceConverter.dto2Entity(resourceDTO));
        return R.success();
    }

    /**
     * 修改模块资源
     */
    @Operation(summary = "修改模块资源")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "resourceId", value = "资源ID"),
            @ApiImplicitParam(name = "resource", value = "资源信息")
    })
    @PutMapping("{moduleId}/resource/{resourceId}")
    public R editResource(@PathVariable Long moduleId, @RequestBody ModuleResourceDTO resourceDTO, @PathVariable Long resourceId) {
        ModuleResource resource = ModuleResourceConverter.dto2Entity(resourceDTO, resourceId);
        resource.setModuleId(moduleId);
        moduleService.editResource(resource);
        return R.success();
    }


}